package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Exam;
import com.ruoyi.system.domain.Paper;
import com.ruoyi.system.domain.PaperSubject;
import com.ruoyi.system.mapper.ExamMapper;
import com.ruoyi.system.mapper.PaperMapper;
import com.ruoyi.system.mapper.PaperSubjectMapper;
import com.ruoyi.system.mapper.SubjectMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Subject;
import com.ruoyi.system.service.ISubjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题库Controller
 *
 * @author ruoyi
 * @date 2022-11-19
 */
@RestController
@RequestMapping("/system/subject")
public class SubjectController extends BaseController
{
    @Autowired
    private ISubjectService subjectService;
    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private PaperSubjectMapper paperSubjectMapper;

    @Autowired
    private SubjectMapper subjectMapper;

    @GetMapping("/timu/{examId}")
    public TableDataInfo timu(@PathVariable Long examId)
    {
        startPage();
        Exam exam = examMapper.selectExamByExamId(examId);
        Long paperId = exam.getPaperId();
        PaperSubject paperSubject=new PaperSubject();
        paperSubject.setPaperId(paperId);
        List<PaperSubject> subjectList = paperSubjectMapper.selectPaperSubjectList(paperSubject);
        List<Subject> subjectList1=new ArrayList<>();
        for (int i = 0; i < subjectList.size(); i++) {
            Long subjectId = subjectList.get(i).getSubjectId();
            Subject subject = subjectMapper.selectSubjectBySubjectId(subjectId);
            subjectList1.add(subject);
        }

        return getDataTable(subjectList1);
    }
    /**
     * 查询题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(Subject subject)
    {
        startPage();
        List<Subject> list = subjectService.selectSubjectList(subject);
        return getDataTable(list);
    }

    /**
     * 导出题库列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Subject subject)
    {
        List<Subject> list = subjectService.selectSubjectList(subject);
        ExcelUtil<Subject> util = new ExcelUtil<Subject>(Subject.class);
        util.exportExcel(response, list, "题库数据");
    }

    /**
     * 获取题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:subject:query')")
    @GetMapping(value = "/{subjectId}")
    public AjaxResult getInfo(@PathVariable("subjectId") Long subjectId)
    {
        return success(subjectService.selectSubjectBySubjectId(subjectId));
    }

    /**
     * 新增题库
     */
    @PreAuthorize("@ss.hasPermi('system:subject:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Subject subject)
    {
        return toAjax(subjectService.insertSubject(subject));
    }

    /**
     * 修改题库
     */
    @PreAuthorize("@ss.hasPermi('system:subject:edit')")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Subject subject)
    {
        return toAjax(subjectService.updateSubject(subject));
    }

    /**
     * 删除题库
     */
    @PreAuthorize("@ss.hasPermi('system:subject:remove')")
    @Log(title = "题库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subjectIds}")
    public AjaxResult remove(@PathVariable Long[] subjectIds)
    {
        return toAjax(subjectService.deleteSubjectBySubjectIds(subjectIds));
    }
}
